﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ChGK.Data;

namespace CHGK.web.UI.Models
{
    public class CompetitionRepositry
    {
        CHGKModelContainer _CHGKContainer;
        public CompetitionRepositry()
        {
            _CHGKContainer = new CHGKModelContainer();

        }

        public CompetitionRepositry(CHGKModelContainer CHGKContainer)
        {
            _CHGKContainer = CHGKContainer;
        }

        public IEnumerable<Competition> GetAllCompetitions()
        {
            return _CHGKContainer.Competitions.OrderByDescending(m => m.ID);
        }

        public Competition GetCompetition(int id)
        {
            Competition dbCompetition = _CHGKContainer.Competitions.SingleOrDefault(t => t.ID == id);
            
            if (dbCompetition == null)
            {
                return null;
            }
            else
            {
                return dbCompetition;
            }

        }

        public Competition GetCompetitionByUserLogin(string UserLogin)
        {
            User CurrentUser = _CHGKContainer.Users.SingleOrDefault(u=>u.Login == UserLogin);

            if (CurrentUser == null)
            {
                return null;
            }
            else
            {
                return GetCompetition(CurrentUser.CompetitionID);
            }
        }

        public Competition GetCompetitionByName(string Name)
        {
            Competition _Competition = _CHGKContainer.Competitions.SingleOrDefault(c => c.Name == Name);

            if (_Competition == null)
            {
                return null;
            }

            return _Competition;
        }

        public Boolean IsCompetitionExist(string Name)
        {
            if (GetCompetitionByName(Name) == null)
            {
                return false;
            }

            return true;
        }
        public void Competition_Adduser(int id, User su)
        {
            Competition dbCompetition = GetCompetition(id);
           
            dbCompetition.Users.Attach(su);
            _CHGKContainer.SaveChanges();
        }

        public string GetName(int id)
        {
            Competition dbCompetition = GetCompetition(id);

            if (dbCompetition == null)
            {
                return null;
            }
            else
            {
                return dbCompetition.Name;
            }
        }

        public Competition CreateCompetition(Competition NewCompetition)
        {
            _CHGKContainer.Competitions.AddObject(NewCompetition);
            _CHGKContainer.SaveChanges();

            return GetCompetitionByName(NewCompetition.Name);
        }

        public void UpdateCompetition(int ID, bool IsEnded)
        {
            var dbCompetition = GetCompetition(ID);
            dbCompetition.IsEnded = IsEnded;

            _CHGKContainer.SaveChanges();
        }
    }
}